package com.dhy.socket.handler;


import com.dhy.calculate.Calculator;
import com.dhy.handle.FileHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 *     通信消息处理
 * </p>
 * @author 大忽悠
 * @create 2022/7/5 10:11
 */
@Slf4j
@ChannelHandler.Sharable
public class DistributeServerInfoHandler extends ChannelInboundHandlerAdapter {
    private static final ExecutorService writeThread= Executors.newFixedThreadPool(3);
    /**
     * 有三个副本连接到主实例上来
     */
    private static final List<Channel> clientChannel=new ArrayList<>();
    /**
     * 计算数据
     */
    private static final Calculator cal=new Calculator();


    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        log.info("副本[{}]实例已经连接到主实例",ctx.channel().remoteAddress());
        clientChannel.add(ctx.channel());
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg){
        log.info("客户端消息为: {}",msg);
        ctx.fireChannelRead(msg);
    }

    /**
     * 发送消息给各个副本实例,并读取PartTblFile
     */
    public void channelWrite(){
        List<String> indexList=FileHandler.getIndex();
        writeThread.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    while(clientChannel.size()!=3){
                           Thread.sleep(100);
                    }
                    log.info("各个副本实例已经准备就绪");
                    for (int i = 1; i <=clientChannel.size(); i++) {
                        Channel client = clientChannel.get(i-1);
                        log.info("发送给客户端[{}]的消息为:{}",client.remoteAddress(),indexList.get(i));
                        client.writeAndFlush(indexList.get(i));
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //主实例读取属于自己的分片数据
        writeThread.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    cal.setLineItemIndexData(FileHandler.readByIndex(indexList.get(0)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        //主实例负责额外读取PartTblFile
        writeThread.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    cal.setPartTblData(FileHandler.readPartTblFile());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }



    public void close() {
        writeThread.shutdown();
    }
}
